package edu.xhu.widget;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import com.bean.StuCourseBean;

import edu.xhu.EduSystemTimeTable;
import edu.xhu.R;
import edu.xhu.ShortCourseActivity;
import edu.xhu.XihuaClientActivity;
import edu.xhu.util.DatabaseAdapter;

import android.R.integer;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.text.format.Time;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

public class CourseWidget extends AppWidgetProvider{

	//远程界面
			public   RemoteViews updateView = null;
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				System.out.println(".....................onReceive............................");
				 super.onReceive(context, intent);  
				 String nRole=intent.getStringExtra("Role");
				 int Role = 2;
				 //System.out.println("..Role..");
				 //System.out.println( Role + ";nRole=" + nRole);
				 if (nRole != null){
					 Role=Integer.parseInt(nRole);
				 }
				 
				 System.out.println(".........Role............");
				 System.out.println( Role);
				 if(intent.getAction().equals("test.course.CLICK")){//对应到EduSystemTimeTable.class中点击同步就更新一下小工具
					System.out.println("onReceive..准备改变widget");
					buildUpdate(context,Role);
					 //更新界面
					 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
					 appWidgetManager.updateAppWidget(new ComponentName(context, CourseWidget.class), updateView); 
				 }
			}

			@Override
			public void onUpdate(Context context,
					AppWidgetManager appWidgetManager, int[] appWidgetIds) {
				// TODO Auto-generated method stub
				super.onUpdate(context, appWidgetManager, appWidgetIds);
				System.out.println(".....................onUpdate............................");
				//创建界面
				int Role=2;
				buildUpdate(context,Role);
				//更新界面
				int count = appWidgetIds.length;
				for(int i=0 ;i<count ; i++){
					System.out.println("id="+appWidgetIds[i]);
					appWidgetManager.updateAppWidget(appWidgetIds[i], updateView);
				}
			}
			/*
			 * 根据用户设置创建远程界面
			 */
			private void buildUpdate(Context context,int nRole) {
				int color;
				//加载布局文件
				updateView = new RemoteViews(context.getPackageName(),R.layout.course_ok);
				//获取今天是星期几
				Calendar cal=Calendar.getInstance();
			    System.out.println(cal.getTime());
			    System.out.println(cal.get(Calendar.DAY_OF_WEEK));
			    int	w =cal.get(Calendar.DAY_OF_WEEK);
			    
			  //获取数据库资源并存入到ArrayList
				DatabaseAdapter dbAdapter = new DatabaseAdapter(context);
				ArrayList<StuCourseBean> courseList = new ArrayList<StuCourseBean>();
				for (int i=0; i<=4; i++){
					for (int j=0; j<=6; j++){
						dbAdapter.openDatabase();
						StuCourseBean bean = dbAdapter.getOneTimeTable(nRole, i + "" + j);
						dbAdapter.closeDatabase();
						if (bean.name != null)
							courseList.add(bean);
					}
				}
				//输出
				Iterator<StuCourseBean> iterator = courseList.iterator();
				while (iterator.hasNext()){
					String courseName="", place="", teacher="", time="";
					StuCourseBean bean = iterator.next();
					/*if(bean.x==0){
						color=Color.argb(200, 54, 49, 193);
						if(bean.y==0){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
								    courseName = bean.name;
								    courseName=get_S(courseName);
								    updateView.setTextViewText(R.id.row1_11, courseName);
								    updateView.setTextColor(R.id.row1_11, color);
								}
						}
						else if(bean.y==1){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row1_21, courseName);
							    updateView.setTextColor(R.id.row1_21, color);
							}
						}
						else if(bean.y==2){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row1_31, courseName);
							    updateView.setTextColor(R.id.row1_31, color);
							}
						}
						else if(bean.y==3){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row1_41, courseName);
							    updateView.setTextColor(R.id.row1_41, color);
							}
						}
						else if(bean.y==4){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row1_51, courseName);
							    updateView.setTextColor(R.id.row1_51, color);
							}
						}
						else if(bean.y==5){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row1_61, courseName);
							    updateView.setTextColor(R.id.row1_61, color);
							}
						}
						else if(bean.y==6){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row1_71, courseName);
							    updateView.setTextColor(R.id.row1_71, color);
							}
						}
						
					}
					else if(bean.x==1){
						color=Color.argb(200, 64, 49, 88);
						if(bean.y==0){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
								    courseName = bean.name;
								    courseName=get_S(courseName);
								    updateView.setTextViewText(R.id.row2_11, courseName);
								    updateView.setTextColor(R.id.row2_11, color);
								}
						}
						else if(bean.y==1){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row2_21, courseName);
							    updateView.setTextColor(R.id.row2_21, color);
							}
						}
						else if(bean.y==2){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row2_31, courseName);
							    updateView.setTextColor(R.id.row2_31, color);
							}
						}
						else if(bean.y==3){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row2_41, courseName);
							    updateView.setTextColor(R.id.row2_41, color);
							}
						}
						else if(bean.y==4){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row2_51, courseName);
							    updateView.setTextColor(R.id.row2_51, color);
							}
						}
						else if(bean.y==5){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row2_61, courseName);
							    updateView.setTextColor(R.id.row2_61, color);
							}
						}
						else if(bean.y==6){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row2_71, courseName);
							    updateView.setTextColor(R.id.row2_71, color);
							}
						}
						
					}
					else if(bean.x==2){
						color=Color.argb(200, 30, 109, 63);
						if(bean.y==0){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
								    courseName = bean.name;
								    courseName=get_S(courseName);
								    updateView.setTextViewText(R.id.row3_11, courseName);
								    updateView.setTextColor(R.id.row3_11, color);
								}
						}
						else if(bean.y==1){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row3_21, courseName);
							    updateView.setTextColor(R.id.row3_21, color);
							}
						}
						else if(bean.y==2){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row3_31, courseName);
							    updateView.setTextColor(R.id.row3_31, color);
							}
						}
						else if(bean.y==3){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row3_41, courseName);
							    updateView.setTextColor(R.id.row3_41, color);
							}
						}
						else if(bean.y==4){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row3_51, courseName);
							    updateView.setTextColor(R.id.row3_51, color);
							}
						}
						else if(bean.y==5){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row3_61, courseName);
							    updateView.setTextColor(R.id.row3_61, color);
							}
						}
						else if(bean.y==6){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row3_71, courseName);
							    updateView.setTextColor(R.id.row3_71, color);
							}
						}
						
					}
					else if(bean.x==3){
						color=Color.argb(200, 19, 200, 106);
						if(bean.y==0){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
								    courseName = bean.name;
								    courseName=get_S(courseName);
								    updateView.setTextViewText(R.id.row4_11, courseName);
								    updateView.setTextColor(R.id.row4_11, color);
								}
						}
						else if(bean.y==1){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row4_21, courseName);
							    updateView.setTextColor(R.id.row4_21, color);
							}
						}
						else if(bean.y==2){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row4_31, courseName);
							    updateView.setTextColor(R.id.row4_31, color);
							}
						}
						else if(bean.y==3){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row4_41, courseName);
							    updateView.setTextColor(R.id.row4_41, color);
							}
						}
						else if(bean.y==4){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row4_51, courseName);
							    updateView.setTextColor(R.id.row4_51, color);
							}
						}
						else if(bean.y==5){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row4_61, courseName);
							    updateView.setTextColor(R.id.row4_61, color);
							}
						}
						else if(bean.y==6){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row4_71, courseName);
							    updateView.setTextColor(R.id.row4_71, color);
							}
						}
						
					}
					else if(bean.x==4){
						color=Color.argb(200, 106, 46, 76);
						if(bean.y==0){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
								    courseName = bean.name;
								    courseName=get_S(courseName);
								    updateView.setTextViewText(R.id.row5_11, courseName);
								    updateView.setTextColor(R.id.row5_11, color);
								}
						}
						else if(bean.y==1){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row5_21, courseName);
							    updateView.setTextColor(R.id.row5_21, color);
							}
						}
						else if(bean.y==2){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row5_31, courseName);
							    updateView.setTextColor(R.id.row5_31, color);
							}
						}
						else if(bean.y==3){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row5_41, courseName);
							    updateView.setTextColor(R.id.row5_41, color);
							}
						}
						else if(bean.y==4){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row5_51, courseName);
							    updateView.setTextColor(R.id.row5_51, color);
							}
						}
						else if(bean.y==5){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row5_61, courseName);
							    updateView.setTextColor(R.id.row5_61, color);
							}
						}
						else if(bean.y==6){
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals("")){
							    courseName = bean.name;
							    courseName=get_S(courseName);
							    updateView.setTextViewText(R.id.row5_71, courseName);
							    updateView.setTextColor(R.id.row5_71, color);
							}
						}
						
					}*/
					
					if (bean.y == w){//代表星期几
						if(bean.x==0)
						{
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals(""))
								//k++;
								courseName = bean.name;
							if (bean.site!=null && !bean.site.equals("null")&&!bean.site.equals(""))
								place = bean.site;
							if (bean.teacher!=null && !bean.teacher.equals("null")&&!bean.teacher.equals(""))
								teacher = bean.teacher;
							if (bean.time!=null && !bean.time.equals("null")&&!bean.time.equals(""))
								time = bean.time;
							updateView.setTextViewText(R.id.course_name1, courseName);
							updateView.setTextViewText(R.id.course_place1, place);
							updateView.setTextViewText(R.id.course_teacher1, teacher);
							updateView.setTextViewText(R.id.course_time1, time);
						}
						if(bean.x==1)
						{
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals(""))
								//k++;
								courseName = bean.name;
							if (bean.site!=null && !bean.site.equals("null")&&!bean.site.equals(""))
								place = bean.site;
							if (bean.teacher!=null && !bean.teacher.equals("null")&&!bean.teacher.equals(""))
								teacher = bean.teacher;
							if (bean.time!=null && !bean.time.equals("null")&&!bean.time.equals(""))
								time = bean.time;
							updateView.setTextViewText(R.id.course_name2, courseName);
							updateView.setTextViewText(R.id.course_place2, place);
							updateView.setTextViewText(R.id.course_teacher2, teacher);
							updateView.setTextViewText(R.id.course_time2, time);
						}
						if(bean.x==2)
						{
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals(""))
								//k++;
								courseName = bean.name;
							if (bean.site!=null && !bean.site.equals("null")&&!bean.site.equals(""))
								place = bean.site;
							if (bean.teacher!=null && !bean.teacher.equals("null")&&!bean.teacher.equals(""))
								teacher = bean.teacher;
							if (bean.time!=null && !bean.time.equals("null")&&!bean.time.equals(""))
								time = bean.time;
							updateView.setTextViewText(R.id.course_name3, courseName);
							updateView.setTextViewText(R.id.course_place3, place);
							updateView.setTextViewText(R.id.course_teacher3, teacher);
							updateView.setTextViewText(R.id.course_time3, time);
						}
						if(bean.x==3)
						{
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals(""))
								//k++;
								courseName = bean.name;
							if (bean.site!=null && !bean.site.equals("null")&&!bean.site.equals(""))
								place = bean.site;
							if (bean.teacher!=null && !bean.teacher.equals("null")&&!bean.teacher.equals(""))
								teacher = bean.teacher;
							if (bean.time!=null && !bean.time.equals("null")&&!bean.time.equals(""))
								time = bean.time;
							updateView.setTextViewText(R.id.course_name4, courseName);
							updateView.setTextViewText(R.id.course_place4, place);
							updateView.setTextViewText(R.id.course_teacher4, teacher);
							updateView.setTextViewText(R.id.course_time4, time);
						}
						if(bean.x==4)
						{
							if (bean.name!=null && !bean.name.equals("null") && !bean.name.equals(""))
								//k++;
								courseName = bean.name;
							if (bean.site!=null && !bean.site.equals("null")&&!bean.site.equals(""))
								place = bean.site;
							if (bean.teacher!=null && !bean.teacher.equals("null")&&!bean.teacher.equals(""))
								teacher = bean.teacher;
							if (bean.time!=null && !bean.time.equals("null")&&!bean.time.equals(""))
								time = bean.time;
							updateView.setTextViewText(R.id.course_name5, courseName);
							updateView.setTextViewText(R.id.course_place5, place);
							updateView.setTextViewText(R.id.course_teacher5, teacher);
							updateView.setTextViewText(R.id.course_time5, time);
						}
					}
				}
				
				//对接设置activity,就是单击事件的activity
			    Intent intent =  new  Intent(context, XihuaClientActivity.class );   
				PendingIntent pendingIntent = PendingIntent.getActivity(context ,  0 ,   
					        intent, 0 );   
				updateView.setOnClickPendingIntent(R.id.ok_c, pendingIntent);//单击R.id.ok_c可以有跳转
				
  }
			private String get_S(String str){
				String str_s="";
				char[] c;
				System.out.print(str);
				c=str.toCharArray();
				for(char ch:c){
					//System.out.print(ch+"\n");
					str_s=str_s+ch+"\n";
				}
				int len=str_s.length();
			    if(len>12){
					str_s=str_s.substring(0, 12);
				    str_s=str_s+"...";
				}
				else{
				    str_s=str_s.substring(0, len-1);
				}
				//System.out.print(str_s);
				return str_s;
			}		
}
